﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public partial class Controls_TaoBao_TaoBaoTB_BaobeiLink : System.Web.UI.UserControl
{
    private string _productCode;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["key"] != null && Request.QueryString["key"].Length > 0)
            _productCode = Request.QueryString["key"];
        if (!IsPostBack)       
            Bind();       
    }
    void Bind()
    {
        AspNetPager1.PageSize = ConfigurationManager.AppSettings["RowCountPerPage"].ToString().Length > 0 ?
                        Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString()) : 10;
        int pageCount = 0;
        int rowCount = 0;
        List<Model.TaoBao.TB_BAOBEI> properties = new BLL.TaoBao.TB_BAOBEI().GetList(this.AspNetPager1.PageSize,
            this.AspNetPager1.CurrentPageIndex, "MID", false, "", out pageCount, out rowCount);
        DataTable dt = new DataTable();
        dt.Columns.Add("MID");
        dt.Columns.Add("field1");
        dt.Columns.Add("BBMC");
        foreach(Model.TaoBao.TB_BAOBEI bb in properties)
            dt.Rows.Add(new string []{bb.MID.ToString(),bb.field1,bb.BBMC});
        gvList.DataSource = dt;
        gvList.DataBind();
        this.AspNetPager1.RecordCount = rowCount;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        int pageCount = 0;
        int rowCount = 0;
        List<Model.TaoBao.TB_BAOBEI> colors = new BLL.TaoBao.TB_BAOBEI().GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex,
            "od_id", false, "", out pageCount, out rowCount);
        DataTable dt = new DataTable();
        dt.Columns.Add("MID");
        dt.Columns.Add("field1");
        dt.Columns.Add("BBMC");
        foreach (Model.TaoBao.TB_BAOBEI bb in colors)
            dt.Rows.Add(new string[] {bb.MID.ToString(),bb.field1,bb.BBMC });
        gvList.DataSource = dt;
        gvList.DataBind(); 
    }
    protected void chkAll_OnCheckedChanged(object sender, EventArgs e)
    {
        new TBCommon().chk_OnCheckedChanged(sender, e, gvList, "chkOne");
    }
    //宝贝和本地商品关联，将本地商品代码写入表tb_baobei.field2
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300100", "00017"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        if (_productCode == null || _productCode.Length == 0)
        {
            new TBCommon().ShowMessage(Page,"当前商品不存在。");
            return;
        }
        foreach(GridViewRow row in gvList.Rows)
        {
            CheckBox chk = (CheckBox)row.FindControl("chkOne");
            if (chk != null && chk.Checked)
                UpdateTBBaobei(Convert.ToInt32(row.Cells[1].Text));
        }
    }
    void UpdateTBBaobei(int mid)
    {
       if(mid>0)
        {
            Model.TaoBao.TB_BAOBEI bb = new BLL.TaoBao.TB_BAOBEI().GetModel(mid);
            if (bb != null)
            {
                bb.field2 = _productCode;
                new BLL.TaoBao.TB_BAOBEI().Update(bb);
            }
        }
    }
    //判断是否已绑定
    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
}
